package design.mode.chapter3.item3.versionPlus.controller;

import design.mode.chapter3.item3.versionPlus.domain.RequestInfo;
import design.mode.chapter3.item3.versionPlus.domain.UserVo;

/**
 * @author ZhongMingde on 2021/10/29
 */

public class UserControllerProxy implements IUserController {
    private MetricsCollector metricsCollector;
    private UserController userController;


    public UserControllerProxy(UserController userController) {
        this.metricsCollector = metricsCollector;
        this.userController = userController;
    }


    @Override
    public UserVo login(String telephone, String password) {
        long startTimestamp = System.currentTimeMillis();
        // 委托
        userController.login(telephone, password);

        long endTimeStamp = System.currentTimeMillis();
        long responseTime = endTimeStamp - startTimestamp;
        RequestInfo requestInfo = new RequestInfo("login", responseTime, startTimestamp);
        metricsCollector.recordRequest(requestInfo);
        return null;
    }

    @Override
    public UserVo register(String telephone, String password) {
        long startTimestamp = System.currentTimeMillis();

        UserVo userVo = userController.register(telephone, password);

        long endTimeStamp = System.currentTimeMillis();
        long responseTime = endTimeStamp - startTimestamp;
        RequestInfo requestInfo = new RequestInfo("register", responseTime, startTimestamp);
        metricsCollector.recordRequest(requestInfo);
        return userVo;
    }
}
